package com.yxw.yx_hotel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxw.yx_hotel.dto.PayStayInMoneyDto;
import com.yxw.yx_hotel.dto.QueryInWaitDto;
import com.yxw.yx_hotel.dto.QueryStayRetreatDto;
import com.yxw.yx_hotel.dto.StayRetreatDto;
import com.yxw.yx_hotel.entity.YxStayInEntity;
import com.yxw.yx_hotel.entity.YxStayRetreatEntity;
import com.yxw.yx_hotel.mapper.StayInMapper;
import com.yxw.yx_hotel.mapper.StayRecordMapper;
import com.yxw.yx_hotel.mapper.StayRetreatMapper;
import com.yxw.yx_hotel.service.StayInService;
import com.yxw.yx_hotel.utils.PageUtils;
import com.yxw.yx_hotel.utils.UserUtil;
import com.yxw.yx_hotel.vo.StayInVo;
import com.yxw.yx_hotel.vo.StayRetreatVo;
import com.yxw.yx_hotel.vo.UserTokenVo;
import com.yxw.yxnet_cd_center.common.dto.PageVO;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yxw.yx_hotel.entity.YxFileEntity;
import com.yxw.yx_hotel.entity.YxRoomBedEntity;
import com.yxw.yx_hotel.mapper.RoomBedMapper;
import com.yxw.yx_hotel.service.FileService;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *  在住名单管理
 * @author liuxin
 * @date 2024.01.22
 */
@Service
public class StayInServiceImpl extends ServiceImpl<StayInMapper, YxStayInEntity> implements StayInService {

    @Autowired
    private UserUtil  userUtil;

    @Autowired
    private StayRecordMapper stayRecordMapper;

    @Autowired
    private StayRetreatMapper stayRetreatMapper;
    
    @Autowired
    private RoomBedMapper  roomBedMapper;
    
    @Autowired
    private FileService  fileService; 
    /**
     * 查询在住名单列表
     * @param dto
     * @return
     */
    @Override
    public PageVO<StayInVo> pageStayInList(QueryInWaitDto dto){
        List<StayInVo> voList=new ArrayList<>();
        UserTokenVo userVo = userUtil.getLoginUserVo();
        Page<YxStayInEntity> page = new Page<>(dto.getCurrPage(),dto.getPageSize());
        LambdaQueryWrapper<YxStayInEntity> queryWrapper=new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(dto.getUserName())){
            queryWrapper.like(YxStayInEntity::getUserName,dto.getUserName().trim());
        }
        if(StringUtils.isNotBlank(dto.getDeptName())){
            queryWrapper.like(YxStayInEntity::getDeptName,dto.getDeptName().trim());
        }
        if(StringUtils.isNotBlank(dto.getRoomName())){
            queryWrapper.like(YxStayInEntity::getRoomName,dto.getRoomName().trim());
        }
        queryWrapper.ge(null!=dto.getInTimeStart(),YxStayInEntity::getStayInTime,dto.getStayTimeStart());
        queryWrapper.le(null!=dto.getInTimeEnd(),YxStayInEntity::getStayInTime,dto.getStayTimeStart());
        queryWrapper.ge(null!=dto.getStayTimeStart(),YxStayInEntity::getStayInTime,dto.getStayTimeStart());
        queryWrapper.le(null!=dto.getStayTimeEnd(),YxStayInEntity::getStayInTime,dto.getStayTimeEnd());
        queryWrapper.eq(YxStayInEntity::getTenantId,userVo.getTenantId());
        queryWrapper.eq(YxStayInEntity::getStatus,1);
        queryWrapper.orderByDesc(YxStayInEntity::getCreateTime);
        Page<YxStayInEntity> entityPage = baseMapper.selectPage(page, queryWrapper);
        List<YxStayInEntity> records = entityPage.getRecords();
        if(null!=records && !records.isEmpty()){
            voList=BeanUtil.copyToList(records,StayInVo.class);
        }
        return PageUtils.get(page, voList);
    }

    /**
     * 查询在住名单详情
     * @param stayInId
     * @return
     */
    @Override
    public StayInVo getStayInById(Long stayInId) {
        StayInVo vo = new StayInVo();
        YxStayInEntity entity = baseMapper.selectById(stayInId);
        if(null!=entity){
            BeanUtil.copyProperties(entity,vo);
        }
        List<YxFileEntity> fileList = fileService.getFilesByBussId(entity.getStayWaitId());
        vo.setPictureList(fileList);
        return vo;
    }

    /**
     * 押金缴纳
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void payStayInMoney(PayStayInMoneyDto dto) {
        if(dto.getMoney().compareTo(0D)<0){
            throw new BaseException("押金必须大于0元");
        }
        UserTokenVo userVo = userUtil.getLoginUserVo();
        YxStayInEntity entity = baseMapper.selectById(dto.getStayIdId());
        if(null==entity){
            throw new BaseException("未查询到在住名单信息");
        }
        if(entity.getPayStatus() == 0)
        {
            throw new BaseException("登记信息显示，不需要缴纳押金");
        }
        LambdaUpdateWrapper<YxStayInEntity> updateWrapper=new LambdaUpdateWrapper();
        updateWrapper.set(YxStayInEntity::getStatus,1);
        updateWrapper.set(YxStayInEntity::getUpdateId,userVo.getUserId());
        updateWrapper.set(YxStayInEntity::getUpdateName,userVo.getNickName());
        updateWrapper.set(YxStayInEntity::getUpdateTime,new Date());
        updateWrapper.set(YxStayInEntity::getMoney,dto.getMoney());
        updateWrapper.eq(YxStayInEntity::getStayInId,dto.getStayIdId());
        baseMapper.update(null,updateWrapper);
    }

    /**
     * 退住
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void stayRetreatByIds(StayRetreatDto dto) {
        //查询在住名单
        YxStayInEntity entity = baseMapper.selectById(dto.getStayIdId());
        if(null==entity){
            throw new BaseException("未查询到在住名单信息");
        }
        UserTokenVo userVo = userUtil.getLoginUserVo();
        YxStayRetreatEntity retreatEntity = new YxStayRetreatEntity();
        BeanUtil.copyProperties(entity,retreatEntity);
        retreatEntity.setCreateId(userVo.getUserId());
        retreatEntity.setCreateName(userVo.getNickName());
        retreatEntity.setMoney(dto.getMoney());
        retreatEntity.setNote(dto.getNote());
        retreatEntity.setRealRetreatTime(new Date());
        retreatEntity.setMonth(this.getMonthDifference(entity.getStayInTime(),new Date()));
        stayRetreatMapper.insert(retreatEntity);
        //修改在住状态
        entity.setUpdateId(userVo.getUserId());
        entity.setUpdateName(userVo.getNickName());
        entity.setUpdateTime(new Date());
        entity.setStatus(0);
        baseMapper.updateById(entity);
        //更新床位待住状态
        LambdaUpdateWrapper<YxRoomBedEntity> updateBedWrapper=new LambdaUpdateWrapper();
        updateBedWrapper.eq(YxRoomBedEntity::getBedId,entity.getBedId());
        updateBedWrapper.set(YxRoomBedEntity::getStatus,2);
        roomBedMapper.update(null, updateBedWrapper);        
    }

    /**
     * 查询退住名单列表
     * @param dto
     * @return
     */
    @Override
    public PageVO<StayRetreatVo> pageStayRetreatList(QueryStayRetreatDto dto) {
        if(null == dto.getUserState())
        {
            dto.setUserState(0);
        }
        List<StayRetreatVo> voList=new ArrayList<>();
        UserTokenVo userVo = userUtil.getLoginUserVo();
        Page<YxStayRetreatEntity> page = new Page<>(dto.getCurrPage(),dto.getPageSize());
        LambdaQueryWrapper<YxStayRetreatEntity> queryWrapper=new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(dto.getUserName())){
            queryWrapper.like(YxStayRetreatEntity::getUserName,dto.getUserName().trim());
        }
        if(StringUtils.isNotBlank(dto.getDeptName())){
            queryWrapper.like(YxStayRetreatEntity::getDeptName,dto.getDeptName().trim());
        }
        if(StringUtils.isNotBlank(dto.getRoomName())){
            queryWrapper.like(YxStayRetreatEntity::getRoomName,dto.getRoomName().trim());
        }
        queryWrapper.ge(null!=dto.getRealRetreatTimeStart(),YxStayRetreatEntity::getRealRetreatTime,dto.getRealRetreatTimeStart());
        queryWrapper.le(null!=dto.getRealRetreatTimeEnd(),YxStayRetreatEntity::getRealRetreatTime,dto.getRealRetreatTimeEnd());
        queryWrapper.eq(YxStayRetreatEntity::getTenantId,userVo.getTenantId());
        queryWrapper.eq((dto.getUserState()>0),YxStayRetreatEntity::getUserState,dto.getUserState());
        queryWrapper.orderByDesc(YxStayRetreatEntity::getCreateTime);
        Page<YxStayRetreatEntity> pageEntity = stayRetreatMapper.selectPage(page, queryWrapper);
        List<YxStayRetreatEntity> entityList = pageEntity.getRecords();
        if(null!=entityList && !entityList.isEmpty()){
            voList=BeanUtil.copyToList(entityList,StayRetreatVo.class);
        }
        return PageUtils.get(page, voList);
    }

    /**
     * 查询退住名单详情
     * @param retreatId
     * @return
     */
    @Override
    public StayRetreatVo getStayRetreatById(Long retreatId) {
        StayRetreatVo vo =new StayRetreatVo();
        YxStayRetreatEntity entity = stayRetreatMapper.selectById(retreatId);
        BeanUtil.copyProperties(entity,vo);
        return vo;
    }

    //------------------------------------------------------------------------------------------------------------------

    /**
     * 计算月份差
     * @return
     */
    private int getMonthDifference(Date startTime,Date endTime){
        //开始时间年月计算
        Calendar sCalendar = Calendar.getInstance();
        sCalendar.setTime(startTime);
        int sYear = sCalendar.get(Calendar.YEAR);
        int sMonth = sCalendar.get(Calendar.MONTH);
        //结束时间年月计算
        Calendar eCalendar = Calendar.getInstance();
        sCalendar.setTime(endTime);
        int eYear = eCalendar.get(Calendar.YEAR);
        int eMonth = eCalendar.get(Calendar.MONTH);
        return (eYear-sYear) * 12 +(eMonth-sMonth);
    }
}
